;MBR
;-------------------------------
%include "boot.inc"
SECTION MBR vstart=0x7c00
mov ax,cs
mov ds,ax
mov es,ax
mov fs,ax
mov ss,ax
mov sp,0x7c00
mov ax,0xb800
mov gs,ax


;Clear screen
;-------------------------------
mov ax,0x0600	;function number ah=0x06
mov bx,0x0700	
mov cx,0	;coordinate topleft(0,0)
mov dx,0x1847	;coordinate downright(80,25)

int 0x10


;Print string MBR
;-------------------------------
mov byte [gs:0x00],'M'
mov byte [gs:0x01],0xA4
mov byte [gs:0x02],'B'
mov byte [gs:0x03],0xA4
mov byte [gs:0x04],'R'
mov byte [gs:0x05],0xA4


;Set parameter
;--------------------------------
mov eax,LOADER_START_SECTOR	;begin LBA addr
mov bx,LOADER_BASE_ADDR		;begin loader addr in memary
mov cx,4			;count of sector
call rd_disk_m_16

jmp LOADER_BASE_ADDR + 0x300


;Function of Read Hard disk
;-------------------------------
rd_disk_m_16:

mov esi,eax	;backup eax
mov di,cx	;backup cx

;read disk
;step 1:set count of sector
mov dx,0x1f2	;port number
mov al,cl
out dx,al

mov eax,esi	;restore eax

;step 2:set LBA addr
;write LBA addr 0~7 into port 0x1f3
mov dx,0x1f3
out dx,al

;write LBA addr 8~15 into port 0x1f4
mov cl,8
shr eax,cl
mov dx,0x1f4
out dx,al

;write LBA addr 16-23 into port 0x1f5
shr eax,cl
mov dx,0x1f5
out dx,al

;write LBA addr 24-27 into port 0x1f6
shr eax,cl
and al,0x0f	;and 00001111
or al,0xe0	;or  11100000   LBA master disk
mov dx,0x1f6
out dx,al

;setp 3 write cmmond code into 0x1f7
mov dx,0x1f7
mov al,0x20
out dx,al

;setp 4 check status of disk
.not_ready:
nop
in al,dx
and al,0x88
cmp al,0x08
jnz .not_ready

;setp 5 read data form port 0x1f0
mov ax,di
mov dx,256
mul dx
mov cx,ax

mov dx,0x1f0
.go_on_read:
in ax,dx
mov [bx],ax
add bx,2
loop .go_on_read
ret

times 510-($-$$) db 0
db 0x55,0xaa























